package com.ruoyi.resource.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.resource.domain.FileInfo;
import com.ruoyi.resource.service.IFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

@RestController
public class FileUploadController extends BaseController {
    @Autowired
    private IFileInfoService fileInfoService;


    @PostMapping("upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        System.out.println(file);
        Long userId = getUserId();//登录的用户ID
        String path1 = "E:\\mall-simple\\ruoyi-admin\\src\\main\\resources\\static\\img\\";
        String path2 = "E:\\mall-simple\\ruoyi-admin\\src\\main\\resources\\static\\video\\";
        String path3 = "E:\\mall-simple\\ruoyi-admin\\src\\main\\resources\\static\\doc\\";
        long size = file.getSize();
        String uuid = UUID.randomUUID().toString().replaceAll("_", "") ;
        String filename = file.getOriginalFilename();//上传原名
        String type = filename.substring(filename.lastIndexOf("."));

        InputStream inputStream = null;
        FileInfo fileInfo = new FileInfo();
        if(type.equals(".png")||type.equals(".jpg")||type.equals(".jpeg")) {
            file.transferTo(new File(path1 + uuid + type));
            inputStream = new FileInputStream(new File(path1 + uuid + type));
            fileInfo.setFileType(1L);
            fileInfo.setHttpPath("http://localhost:8080/img/"+uuid+type);
            fileInfo.setParentId(1L);
            fileInfo.setFilePath(path1);


        } else if(type.equals(".mp4")||type.equals(".wmv")) {
            file.transferTo(new File(path2 + uuid + type));
            inputStream = new FileInputStream(new File(path2 + uuid + type));
            fileInfo.setFileType(2L);
            fileInfo.setHttpPath("http://localhost:8080/video/"+uuid+type);
            fileInfo.setParentId(2L);
            fileInfo.setFilePath(path2);
        }else {
            file.transferTo(new File(path3 + uuid + type));
            inputStream = new FileInputStream(new File(path3 + uuid + type));
            fileInfo.setFileType(0L);
            fileInfo.setHttpPath("http://localhost:8080/doc/"+uuid+type);
            fileInfo.setParentId(0L);
            fileInfo.setFilePath(path3);
        }

        //获取文件MD5
        byte[] bytes=inputStream.readAllBytes();
        String md5= DigestUtils.md5DigestAsHex(bytes);



        fileInfo.setFileSize(String.valueOf(size));
        fileInfo.setFileName(filename);
        fileInfo.setMd5(md5);
        fileInfo.setCrateDate(new Date());
        fileInfo.setUserId(userId);
        fileInfo.setState(1);

        FileInfoController fileInfoController = new FileInfoController();
        fileInfoService.insertFileInfo(fileInfo);
        return AjaxResult.success("上传成功", Map.of("url","http://localhost:8080/static/"+uuid+type,"name",filename));//返回的结果集
    }
}
